<?php
// 区间调试开始
function debug_start($label='')
{
	$GLOBALS[$label]['_beginTime'] = microtime(TRUE);
	if ( IS_MEMORY )  $GLOBALS[$label]['_beginMem'] = memory_get_usage();
}

// 区间调试结束，显示指定标记到当前位置的调试
function debug_end($label='')
{
	$GLOBALS[$label]['_endTime'] = microtime(TRUE);
	echo '<div style="text-align:center;width:100%">Process '.$label.': Times '.number_format($GLOBALS[$label]['_endTime']-$GLOBALS[$label]['_beginTime'],6).'s ';
	if ( IS_MEMORY )  {
		$GLOBALS[$label]['_endMem'] = memory_get_usage();
		echo ' Memories '.number_format(($GLOBALS[$label]['_endMem']-$GLOBALS[$label]['_beginMem'])/1024).' k';
	}
	echo '</div>';
}

// 编译文件
function compile($filename,$runtime=false) {
	if(defined('RUN_TMP') && RUN_TMP==true) return '';
	$content = file_get_contents($filename);
	if(true === $runtime)
	// 替换预编译指令
	$content = preg_replace('/\/\/\[RUNTIME\](.*?)\/\/\[\/RUNTIME\]/s','',$content);
	$content = substr(trim($content),5);
	if('?>' == substr($content,-2))
	$content = substr($content,0,-2);
	return $content;
}

// 去除代码中的空白和注释
function strip_whitespace($content) {
	$stripStr = '';
	//分析php源码
	$tokens =   token_get_all ($content);
	$last_space = false;
	for ($i = 0, $j = count ($tokens); $i < $j; $i++)
	{
		if (is_string ($tokens[$i]))
		{
			$last_space = false;
			$stripStr .= $tokens[$i];
		}
		else
		{
			switch ($tokens[$i][0])
			{
				//过滤各种PHP注释
				case T_COMMENT:
				case T_DOC_COMMENT:
					break;
					//过滤空格
				case T_WHITESPACE:
					if (!$last_space)
					{
						$stripStr .= ' ';
						$last_space = true;
					}
					break;
				default:
					$last_space = false;
					$stripStr .= $tokens[$i][1];
			}
		}
	}
	return $stripStr;
}